const Base = require('../base.js');

module.exports = class extends Base {
    indexAction() {
        this.meta_title = '用户管理';
        this.action_id = 'user';
        return this.display();
    }

    async listAction() {
        let posts = this.post();
        let limits = parseInt(posts.limit);
        let offsets = parseInt(posts.offset + 1);
        //let pageNumbers = parseInt(gets.pageNumber);
        let key = posts.search;
        if (key != null) {
            const userList = await this.model('user').where({u_name: ['like', '%' + key + '%']}).page(offsets, limits).countSelect();
            return this.json(userList);
        } else {
            const userList = await this.model('user').page(offsets, limits).countSelect();
            return this.json(userList);
        }
        //return this.display();
    }

    async addAction() {
        if (this.isPost) {
            const data = this.post();

            //return this.json({success:true});
            let res = true;
            if (res) {
                return this.json({errno:0,title:'成功',message: '保存成功!页面即将自动跳转'});
            } else {
                return this.json({errno: 1000, title: '数据错误!', message: '系统接收错误'});
            }
        }
        else {
            return this.display();
        }
    }

    async editAction() {
        if (this.isPost) {
            const data = this.post();

            //return this.json({success:true});
            let res = false;
            if (res) {
                return this.json({errno:0,title:'成功',message: '保存成功!页面即将自动跳转'});
            } else {
                return this.json({errno: 1000, title: '数据错误!', message: '系统接收错误'});
            }
        }
        else {
            let ids = parseInt(this.get('id'));
            //let finddata = await this.model('user').field('id,u_name,u_title,u_group,u_tf').where({id:ids}).select();//.where({id:ids});
            let finddata = await this.model('user').field('id,u_name,u_title,u_group,u_tf').find(ids);//.where({id:ids});
            //this.assign('menu', finddata);
            //this.meta_title = '编辑目录[' + finddata.m_title + ']';
            if (!think.isEmpty(finddata)) {
                return this.json(finddata);
            }
            else {
                return this.json();
            }
        }
    }
    async delAction(){
        const ids = this.get('id');
        // console.log(id);
        let res;
        // 判断是否是管理员，如果是不能删除;
        if (ids==1) {
            return this.fail({errno: 1000, title: '错误!', message: '管理员不能被删除!'});
        } else {
            // res = await this.db.where({id: id}).delete();
            // 逻辑删除
            //res = await this.db.where({id: ['IN', ids]}).update({status: -1});
            let res = true;
            if (res) {
                return this.json({errno:0,title: '成功',message:'删除成功!'});
            } else {
                return this.json({errno: 1000, title: '错误!', message: '删除失败'});
            }
        }
    }
};
